package com.gzc.service.imp;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.gzc.common.Pager;
import com.gzc.common.SystemContext;
import com.gzc.dao.IMessageDao;
import com.gzc.dao.IUserDao;
import com.gzc.model.Attachment;
import com.gzc.model.Message;
import com.gzc.model.User;
import com.gzc.model.UserReceivedMessage;
import com.gzc.service.IMessageService;

@Service("messageService")
public class MessageService implements IMessageService {
	
	private IUserDao userDao;
	private IMessageDao messageDao;
	
	@Resource
	public void setMessageDao(IMessageDao messageDao) {
		this.messageDao = messageDao;
	}
	@Resource
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	
	@Override
	public void add(Message msg, Integer[] userIds) {
		//1.添加私人信件对象
		//2.设置该信件的发件人为当前登录用户
		//3.添加一组收件人
		msg.setUser(SystemContext.getLoginUser());
		msg.setCreateDate(new Date());
		messageDao.add(msg);
		
//		根据用户id获取一组用户信息
		List<User> users = userDao.listByIds(userIds);
		UserReceivedMessage um = null;
//		为这组用户添加相应的信息
		for(User u : users){
			um = new UserReceivedMessage();
			um.setIsRead(0);
			um.setMessage(msg);
			um.setUser(u);
			messageDao.addObj(um);
		}
//		String[] newNames = DocumentUtil.addAttach(ad, attachmentDao, msg, null);
//		if(isEmail>0) {
//			UserEmail ue = userDao.loadUserEmail(SystemContext.getLoginUser().getId());
//			//将发送邮件放置到一个线程中，这样就是实现了异步处理
//			taskExecutor.execute(new SendMailThread(ue,msg, userIds, ad,newNames,SystemContext.getRealPath()));
//		}
	}
	@Override
	public void deleteReceive(int msgId) {
		// TODO 自动生成的方法存根
		
	}
	@Override
	public void deleteSend(int msgId) {
		// TODO 自动生成的方法存根
		
	}
	@Override
	public Message updateRead(int msgId, int isRead) {
		if(0 == isRead){
			User user = SystemContext.getLoginUser();
			UserReceivedMessage um = messageDao.loadUserMessage(user.getId(), msgId);
			if(0 == um.getIsRead()){
				//如果没有读过，需要更新为已读
				um.setIsRead(1);
				messageDao.updateObj(um);
			}
		}
		return messageDao.load(msgId);
	}
	
	@Override
	public Pager<Message> findSendMessage(String con) {
		User user = SystemContext.getLoginUser();
		//因为：String hql = " select new  com.domain.Message(字段,字段) from Message as msg";所以Message类必须要有接受2个参数的构造函数
		String hql = "select new Message(msg.id, msg.title, msg.content, msg.createDate) from Message as msg where msg.user.id=?";
		
		if (con != null && !"".equals(con.trim())) {
			hql += " and (msg.title like ? or msg.content like ?) order by msg.createDate desc";
			return messageDao.find(hql, new Object[] { user.getId(),
					"%" + con + "%", "%" + con + "%" });
		}
		hql += " order by msg.createDate desc";
		return messageDao.find(hql, user.getId());
	}
	
	@Override
	public Pager<Message> findReceiveMessage(String con, int isRead) {
		User user = SystemContext.getLoginUser();
		String hql = "select um.message from UserReceivedMessage as um left join fetch um.message.user mu"
				+ " left join fetch mu.department where um.isRead=? and um.user.id=?";
		
		if (con != null && !"".equals(con.trim())) {
			hql += " and (um.message.title like ? or um.message.content like ?) order by um.message.createDate desc";
			return messageDao.find(hql, new Object[] { isRead, user.getId(),
					"%" + con + "%", "%" + con + "%" });
		}
		hql += " order by um.message.createDate desc";
		return messageDao.find(hql, new Object[] { isRead, user.getId() });
	}
	
	@Override
	public List<Attachment> listAttachmentByMsg(int msgId) {
		final String hql = "from Attachment att where att.message.id=?";
		return null; // attachmentDao.list(hql,msgId);
	}

}
